package com.cy.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cy.bfzcommons.mapper.AdministratorMapper;
import com.cy.bfzcommons.pojo.AdminPassword;
import com.cy.bfzcommons.pojo.Administrator;
import com.cy.bfzcommons.service.AdministratorService;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class AdministratorServiceImpl implements AdministratorService {

    @Autowired
    private AdministratorMapper administratorMapper;

//    @Autowired
//    private AdministratorRoleMapper administratorRoleMapper;
//
//    @Autowired
//    private RoleMapper roleMapper;



//    @Override
//    public String saveAdmin(Administrator administrator) {
//        if (StringUtil.isEmpty(administrator.getUsername())) return "用户名不能为空";
//        if (StringUtil.isEmpty(administrator.getPassword())) return "密码不能为空";
//        if (StringUtil.isEmpty(administrator.getEmail())) return "邮箱不能为空";
//        if (StringUtil.isEmpty(administrator.getPhone())) return "电话号码不能为空";
//
//        String salt = UUID.randomUUID().toString().replace("-", "");
//        SimpleHash simpleHash = new SimpleHash("MD5",administrator.getPassword(),salt,1);
//        String hex = simpleHash.toHex();
//        administrator.setSalt(salt);
//        administrator.setPassword(hex);
//        administratorMapper.insert(administrator);
//
//        List<Integer> roleList = administrator.getRoleList();
//
//        //新增管理员   并给管理员赋予权限   为中间表增加数据
//        if (roleList != null && roleList.size()>0) {
//            for (Integer roleId : roleList) {
//                //遍历所有的数据然后进行数据插入
//                AdministratorRole administratorRole = new AdministratorRole();
//                administratorRole.setAdministratorId(administrator.getAdministratorId());
//                administratorRole.setRoleId(roleId);
//                administratorRoleMapper.insert(administratorRole);
//            }
//        }else {
//            return "请为新增管理员赋予权限";
//        }
//
//        return "新增成功";
//    }

    @Override
    public String saveAdmin(Administrator administrator) {
        return null;
    }

    @Override
    public void updateAdmin(Administrator administrator) {

    }

//    @Override
//    public void updateAdmin(Administrator administrator) {
//       // List<Integer> roleList = administrator.getRoleList();
//        //通过前端页面拿到该管理的所属角色
//        List<String> roleNames = administrator.getRoleName();
//        //判断list集合是否为空
//        //如果是空  则不需要修改中间表     如果不是空删除中间表中所有该管理员的数据   再进行新增
//        if (roleNames!=null && roleNames.size()>0) {
//            QueryWrapper<AdministratorRole> queryWrapper = new QueryWrapper();
//            queryWrapper.eq("administrator_id", administrator.getAdministratorId());
//            administratorRoleMapper.delete(queryWrapper);
//
//            //遍历拿到该角色的id  然后进行新增
//            for (String roleName : roleNames) {
//                QueryWrapper<Role> queryWrapper1 = new QueryWrapper<>();
//                queryWrapper1.select("role_id").eq("role_Name", roleName);
//                Role role = roleMapper.selectOne(queryWrapper1);
//                AdministratorRole administratorRole = new AdministratorRole();
//                administratorRole.setAdministratorId(administrator.getAdministratorId());
//                administratorRole.setRoleId(role.getRoleId());
//                administratorRoleMapper.insert(administratorRole);
//            }
//
//        }
//        administratorMapper.updateById(administrator);
//    }

    @Override
    public List<Administrator> findAdmin(String username) {
        QueryWrapper<Administrator> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!StringUtil.isEmpty(username), "username", username);
        return administratorMapper.selectList(queryWrapper);

    }

    @Override
    public String updatePassword(AdminPassword adminPassword) {
        return null;
    }

    @Override
    public Administrator selectById(Integer administratorId) {
        return administratorMapper.selectById(administratorId);
    }

//    @Override
//    public String updatePassword(AdminPassword passWord) {
//        QueryWrapper<Administrator> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("username", passWord.getUsername());
//        Administrator administrator = administratorMapper.selectOne(queryWrapper);
//        if(passWord.getUsername() == "" || passWord.getUsername()==null){
//            return "请输入用户名";
//        }
//        if (administrator==null) return "无该用户";
//
//        String password = passWord.getPassword().trim();
//        String newPassword = passWord.getNewPassword().trim();
//
//        if (StringUtil.isEmpty(password) || StringUtil.isEmpty(newPassword)) return "请输入旧密码与新密码";
//
//        if (passWord.equals(newPassword)) return "新密码不能与旧密码重复";
//
//        String salt= administrator.getSalt();                                                             //获取盐值
//        SimpleHash shold = new SimpleHash("MD5" , password , salt ,1);     //获取用户的旧密码
//        String md5pwOld = shold.toHex();     //用户传入的旧密码  根据 数据库盐值 加密后的字符串
//        String oldpassword = administrator.getPassword();
//        if (oldpassword.equals(md5pwOld)){
//            Administrator administrator1 = new Administrator();
//            String newSalt = UUID.randomUUID().toString().replace("-", "");
//            SimpleHash simpleHash = new SimpleHash("MD5",newPassword,newSalt,1); //新密码加密
//            String toHex = simpleHash.toHex();
//            administrator1.setAdministratorId( administrator.getAdministratorId() ).setSalt( newSalt ).setPassword( toHex );
//            System.out.println(administrator.getAdministratorId());
//            administratorMapper.updateById(administrator1);
//            return "修改密码成功";
//        }else {
//            return "旧密码有误,请重新输入";
//        }
//
//
//    }

//    /**
//     * 查询出该id的数据  还有封装了该管理员的角色名
//     * @param administratorId
//     * @return
//     */
//    @Override
//    public Administrator selectById(Integer administratorId) {
//        Administrator administrator = administratorMapper.selectById(administratorId);
//        List<String> roleNameList = administratorRoleMapper.selectName(administratorId);
//        administrator.setRoleName(roleNameList);
//        return administrator;
//    }

    @Override
    public String loginAdministrator(String userName,String password) {
        QueryWrapper<Administrator> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("user_name",userName);
        Administrator administrator = administratorMapper.selectOne(queryWrapper);
        if (administrator==null){
            return "密码或者用户名有误";
        }
        return "登录成功";
    }

    @Override
    public Administrator selectUsername(String userName) {
        QueryWrapper<Administrator> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", userName);
        Administrator administrator = administratorMapper.selectOne(queryWrapper);

        return administrator;
    }

    @Override
    public List<Object> selectName() {
        QueryWrapper<Administrator> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("username");
        List<Object> list = administratorMapper.selectObjs(queryWrapper);
        return list;
    }

    @Override
    public Set<String> SelectPermission(Integer administratorId) {
        return administratorMapper.SelectPermission(administratorId);
    }

//    @Override
//    public Administrator selectByName(String username) {
//        QueryWrapper<Administrator> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("username", username);
//        Administrator administrator = administratorMapper.selectOne(queryWrapper);
//        QueryWrapper<Administrator> queryWrapper1 = new QueryWrapper<>();
//        queryWrapper1.eq("administrator_id", administrator.getAdministratorId());
//        Ad
//        return null;
//    }


}
